<?php
/**
 * @name /www/unit-tests/User.php
 * @author Michael Foss <michael@mikefoss.com>
 * @version 2011.03.26
 *
 * Unit test for User class.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
// Libraries
ReqLib('/lib/private/Item.php');

function DefineHeader() {
?><style type="text/css">
  code {
    font: bold 10pt 'Courier New';
    color: #666;
    background-color: #CCC;
    border: 3px solid #666;
    padding: 4px 6px;
    margin: 10px 20px;
    display: block;
  }
  form, ul, ol {
    margin: 0px 20px;
  }
  a#Back {
    float: right;
    margin: 10px;
    padding: 4px 6px;
    border: 1px solid #666;
    background-color: #CCC;
  }
</style><?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
/* Methods:

	Item()

  "GET":
	Item[] GetItems(int offset, int maxResults)
	bool Exists(int itemId)
	string ToString()

  "SCUD":
	bool Load(int itemId)
	bool Create(name)
	bool Save()
	static bool Delete(int itemId)

  Custom:
  GetItemIdByItemName(name)
  CheckItemValidation(ItemId)
  TagItem($tag)
  UntagItem($tag)
  HasTags($item)
  GetTags()
  CreateTag($tagname)
  DeleteTag(tagname)
  ExistsTag(tagname)

*/
  $actions = array(
    'Delete(id)',
    'Delete All',
    'Create',
    'View Details',
    'List(offset,max)',
    'GetIdByItemName(name)',
    'Save',
	'TagItem(id,tag)',
	'UntagItem(id,tag)',
	'HasTag(id,tag)',
	'GetTags()',
    'CreateTag(tagname)',
    'DeleteTag(tagname)',
	'FilteredSearch($query, $tags, false)',
	'FilteredSearch($query, $tags, true)',
	'FilteredSearch($query, $tags)'
  );
  // Page content goes here.
  ?>
<h1><a href="Item.php">Item - Unit Test</a></h1>
<a id="Back" href="index.php">Back to Unit Testing Home</a>
<form method="GET">
  <label for="Param1">Param 1: </label><input type="text" value="<?=htmlspecialchars($_GET['Param1']) ?>" name="Param1" /><br />
  <label for="Param2">Param 2: </label><input type="text" value="<?=htmlspecialchars($_GET['Param2']) ?>" name="Param2" /><br />
<?
  foreach ($actions as $action)
    println('<input type="submit" name="Action" value="' . $action . '" /><br />');
?><br />
<hr />
<?
  DoAction();
?></form>
<?
}

function DoAction() {
?><p>
<?
  switch ($_GET['Action']) {
    case 'Delete(id)':
      println(Item::Delete($_GET['Param1']) ? 'Deleted' : 'Not deleted');
      break;

    case 'Delete All':
      $items = Item::GetItems();
      foreach($items as $item)
        Item::Delete($item->ItemId);
      println('Data cleared.');
      break;

    case 'Create':
      $i = new Item();
      $is = array(
        array(''),
        array(rand(1,10)),
        array(rand(1,10)),
        array(rand(1,10)),
        array(rand(1,10)),
        array(rand(1,10))
      );
      foreach ($is as $data)
        println("Creating item (name:'{$data[0]}'): " . ($i->Create($data[0]) ? 'Created' : 'Not created') . '<br />');
      break;

    case 'List(offset,max)':
      println('<ol>');
      foreach((Item::GetItems($_GET['Param1'], $_GET['Param2'])) as $i)
        println("<li>{$i->ItemId}: {$i->Name}</li>");
      println('</ol>');
      break;

    case 'View Details':
      println('<textarea style="width: 600px; height: 400px;">' . print_r(Item::GetItems(0,1), true) . '</textarea>');
      break;

    case 'GetIdByItemName(name)':
      println("ByName('" . htmlspecialchars($_GET['Param1']) . "') = '" . Item::GetItemIdByItemName($_GET['Param1']) . "'");
      break;

    case 'CheckItemValidation(ItemId)':
      println("Validation 1==validated 0== not('" . htmlspecialchars($_GET['Param1']) . "') = '" . Item::CheckItemValidation($_GET['Param1']) . "'");
      break;

    case 'Save':
      $i = Item::GetItems(0, 20);
      if (!count($i)) {
        $i = new Item();
        $i->Create(rand(1,10));
      }
      else
		$i = $i[0];
      $i->Name = rand(1, 100);
      $i->Verified = rand(0,1);
      $i->Notes = rand(1, 100);
      $i->Quantity = rand(1, 100);
      $i->EstimatedValue = rand(0, 100);
      println($i->Save() ? 'Saved' : 'Not saved');
      println('<br /><textarea style="width: 600px; height: 400px;">Name: ' . $i->Name . "\r\n" . print_r($i->ToString("\r\n"), true) . '</textarea>');
      break;

    case 'HasTag(id,tag)':
      $i = new Item();
      if (!$i->Load($_GET['Param1']))
        println('Could not load');
      else
        println($i->HasTag($_GET['Param2']) ? 'Has tag' : 'Does not have tag');
      break;

    case 'TagItem(id,tag)':
      $i = new Item();
      if (!$i->Load($_GET['Param1']))
        println('Could not load');
      else
        println($i->TagItem($_GET['Param2']) && $i->Save() ? 'Tagged' : 'Not tagged');
      break;

    case 'UntagItem(id,tag)':
      $i = new Item();
      if (!$i->Load($_GET['Param1']))
        println('Could not load');
      else
        println($i->UntagItem($_GET['Param2']) && $i->Save() ? 'Untagged' : 'Still tagged');
      break;

    case 'GetTags()':
      $tags = Item::GetTags();
      println('<br /><textarea style="width: 600px; height: 400px;">' . print_r($tags, true) . '</textarea>');
      break;

    case 'CreateTag(tagname)':
      println(Item::CreateTag($_GET['Param1']) ? 'Created' : 'Not created');
      break;

    case 'DeleteTag(tagname)':
      println(Item::DeleteTag($_GET['Param1']) ? 'Deleted' : 'Not deleted');
      break;

	case 'FilteredSearch($query, $tags, false)':
	case 'FilteredSearch($query, $tags, true)':
	case 'FilteredSearch($query, $tags)':
	  $query = $_GET['Param1'];
	  $tags = trim($_GET['Param2']);
	  if ($tags) {
		$tags = explode(',', $tags);
		array_walk($tags, function(&$t) {
			$t = trim($t);
		});
	  }
	  else
	    $tags = array();
	  print("Printing items:<br />");
	  switch ($_GET['Action']) {
		case 'FilteredSearch($query, $tags, false)':
		  $items = Item::FilteredSearch($query, $tags, false);
		  break;
		case 'FilteredSearch($query, $tags, true)':
		  $items = Item::FilteredSearch($query, $tags, true);
		  break;
		case 'FilteredSearch($query, $tags)':
		  $items = Item::FilteredSearch($query, $tags);
		  break;
	  }
	  foreach ($items as $item)
		println($item->Name . "<br />");
	  break;

    default:
      break;
  }
?></p>
<?
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');